package com.wwwarehouse.taskcenter.tools;

import android.text.InputFilter;
import android.text.Spanned;

/**
 * @创建者 chenchao.
 * @创建时间 2017/9/14 9:20
 * @描述 ${TODO}
 */

public class UserCenterCheckUtils {

    public static CharSequence mCharSequence;
    public static InputFilter limitLength(final int maxLen) {
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence src, int start, int end, Spanned dest, int dstart, int dend) {
                int dindex = 0;
                int count = 0;

                while (count <= maxLen && dindex < dest.length()) {
                    char c = dest.charAt(dindex++);
                    if (c < 128) {
                        count = count + 1;
                    } else {
                        count = count + 2;
                    }
                }

                if (count > maxLen) {
                    return dest.subSequence(0, dindex - 1);
                }

                int sindex = 0;
                while (count <= maxLen && sindex < src.length()) {
                    char c = src.charAt(sindex++);
                    if (c < 128) {
                        count = count + 1;
                    } else {
                        count = count + 2;
                    }
                }

                if (count > maxLen) {
                    sindex--;
                }
                mCharSequence = src.subSequence(0, sindex);
                return mCharSequence;
            }
        };
        return filter;
    }
}
